<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- <link rel="stylesheet" href="../font/iconfont.css"> -->
    <script src="../js/cookie.js"></script>
    <style>
        .createCode {
            user-select: none;
        }

        .right {
            font-size: 12px;
            color: green;
        }

        .error {
            font-size: 12px;
            color: red;
        }
    </style>
</head>

<body>
    <div class="box">
        <p>
            <label for="">用户名:</label>
            <input type="text" class="user">
            <span class="user_span"></span>
        </p>
        <p>
            <label for="">密&emsp;码:</label>
            <input type="text" class="pwd">
            <span class="pwd_span"></span>
        </p>

        <p>
            <input class="btn" type="submit" value="登录">
        </p>
    </div>
</body>

<script>

    var userInp = document.querySelector(".user");
    var userSpan = document.querySelector(".user_span");
    var pwdInp = document.querySelector(".pwd");
    var pwdSpan = document.querySelector(".pwd_span");
    var btn = document.querySelector(".btn");


    btn.onclick = function () {
        // 用户输入的
        var user = userInp.value; // "a123123"
        var pwd = pwdInp.value;

        // [{ user: 'a12312', pwd: '3123123', phone: '17386141517', email: '123123@qq.com' },
        // { user: 'a123123', pwd: '123123', phone: '17386141516', email: '1231234@qq.com' },
        // { user: 'a1231233', pwd: '123123', phone: '17386141512', email: '312332@qq.com' }]

        var userInfo = getCookie("userInfo") ? JSON.parse(getCookie("userInfo")) : [];
        console.log(userInfo);

        // 从数组中找到满足条件的数据?
        // var flag = userInfo.some(info => info.user == user && info.pwd == pwd);

        var info = userInfo.find(info => info.user == user);
        if (info) {
            if (info.pwd === pwd) {
                setCookie("lgc", user, 31 * 24 * 60 * 60);
                location.href = "./index.html";
            } else {
                alert("用户名或密码有误!")
            }

        } else {
            alert("该用户未注册!")
        }

    }


</script>

</html>